package com.tianyadu.mmall.core.validate.code.service.impl;

import com.tianyadu.mmall.core.conf.SecurityConstantsConfig;
import com.tianyadu.mmall.core.validate.code.ValidateCode;
import com.tianyadu.mmall.core.validate.code.service.AbstractValidateCodeProcessor;
import com.tianyadu.mmall.core.validate.code.service.SmsCodeSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * @author xueyj
 * @date 2019-11-14 00:35
 */
@Component
public class SmsValidateCodeProcessor extends AbstractValidateCodeProcessor<ValidateCode> {

    /**
     * 短信验证码发送器
     */
    @Autowired
    private SmsCodeSender smsCodeSender;

    /**
     * 发送校验码，由子类实现
     *
     * @param request      spring工具类，封装请求/响应信息
     * @param validateCode 短信验证信息
     * @return void
     * @author xueyj
     * @date 2019/11/14-0:36
     */
    @Override
    protected void send(ServletWebRequest request, ValidateCode validateCode) throws Exception {
        //  接收短信验证码参数名
        String paramName = SecurityConstantsConfig.DEFAULT_PARAMETER_NAME_MOBILE;
        //  获取请求中手机号码信息
        String mobile = ServletRequestUtils.getRequiredStringParameter(request.getRequest(), paramName);
        //  短信发送验证
        smsCodeSender.sendSms(mobile, validateCode.getCode());
    }
}